package com.tianqu.tms.www.bid.controller;

import java.util.Date;
import java.text.ParseException;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.AucCarInfo;
import com.tianqu.tms.entity.AucManagement;
import com.tianqu.tms.entity.CarImage;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.www.bid.viewhelper.BidCarQuotationLiveDetailViewHelper;

public class BidCarQuotationLiveDetailController extends TmsController {
	
	private Logger logger = Logger.getLogger(BidCarQuotationLiveDetailController.class);

	BidCarQuotationLiveDetailViewHelper view = new BidCarQuotationLiveDetailViewHelper();

	@Override
	public void execute(UserContext uc) throws Exception {

		logger.debug(this.getClass().getName() + "#execute was called.");
		
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			Codes codes = Codes.getInstance();
			
			// 竞价管理
			AucManagement aucManage = AucManagement.getInstance();
			// 竞价车辆信息
			AucCarInfo aucCarinfo = AucCarInfo.getInstance();
			// 车辆款式
			CarStyle carStyle = CarStyle.getInstance();

			// 图片信息
			CarImage aucCarImage = CarImage.getInstance();

			// 车辆ID
			int id = StringUtil.getint(uc.getParameter("id"));
			
			// 根据车辆ID取得车辆信息
			AucManagement auc = aucManage.getAucInfoById(id);
			
			BidCarQuotationLiveDetailViewHelper view = new BidCarQuotationLiveDetailViewHelper();
			
			aucCarinfo = aucCarinfo.getaucCarBase(auc.getSerial());
			
			//竞价表主键ID
			view.setCarId(StringUtil.getString(id));
			
			// 车辆ID
			view.setSerial(aucCarinfo.getSerial());
			
			// 车辆名称
			view.setCarName(aucCarinfo.getCarName());
			
			// 车架号只显示后6位 modify by mufeiyu
			view.setVin(aucCarinfo.getVin().substring(aucCarinfo.getVin().length() - 6, aucCarinfo.getVin().length()));
			
			// 最高出价(元)
			if (auc.getFinalExpetPrices() == 0) {
				view.setFinalExpetPrices("0");
			} else {
				view.setFinalExpetPrices(StringUtil.transYtoW(auc.getFinalExpetPrices()));				
			}
			
			// 出价商户数量
			view.setExpetPricesNum(auc.getExpetPricesNum());

			// 竞价状态
			view.setBidStatus(auc.getBidStatus());
			
			// 竞价开始时间
			Date startTime = new Date(auc.getStartTime().getTime());
			
			// 竞价时长
			int auctionDeadline = auc.getAuctionDeadline();
			
			// 剩余时间（秒）
			String[] djs = getdjs(startTime,auctionDeadline);
			view.setDjsValue(djs[0]);
			view.setDjsUnit(djs[1]);

			// 竞价状态
			if ("0".equals(djs[0])) {
				view.setBidStatusName("结　束");
			} else {
				view.setBidStatusName("开　始");
			}
			
			// 车身颜色
			view.setColor(codes.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COLOR, aucCarinfo.getColor()));
			
			// 年检日期
			view.setYearCarefulPeriod(aucCarinfo.getYearCarefulPeriod());
			
			// 行驶里程
			view.setMileage(StringUtil.getPointBetweenThree(aucCarinfo.getMileage()));
			
			// 登记日期
			view.setRegDate(aucCarinfo.getRegDate());
			
			// 交强险日期
			view.setJqx(aucCarinfo.getJqx());
					
			// 排量
			view.setDisplacement(String.valueOf(carStyle.getExhaustByCarStyleId(aucCarinfo.getCarStyleId())));
			
			// 变速箱
			view.setGearbox(codes.getItemValueByItemIdAndItemCode(Codes.GEARBOX_TYPE, carStyle.getGearboxByCarStyleId(aucCarinfo.getCarStyleId())));
			
            //图片路径
			view.setCarImage(aucCarImage.imgPath(aucCarinfo.getId(), String.valueOf(1), String.valueOf(1),3));

			logger.debug(this.getClass().getName() + "#execute was completed.");
			
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			controller.commit();

		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");

	}

	/**
	 * 取得剩余时间（秒）
	 * 
	 * @param start 竞价开始时间
	 * @param auctionDeadline 竞价时长
	 * @return String[]
	 */
	private String[] getdjs(Date start,int auctionDeadline) throws ParseException {
			
		// 当前时间
		Date nowDate = new Date();
		
		// 开始时间  + 竞价时长 + 1分钟
		Date deadDate= DateUtil.string101ToDate(DateUtil.getTimeByMinute(start,auctionDeadline + 1));
		
		// 剩余时间
		long djs = (deadDate.getTime() - nowDate.getTime())/1000;
		
		String[] djsStr = {"0","秒"};
		
		if (djs < 0) {
			
			// 剩余时间60秒以内			
			djsStr[0] = "0";
			djsStr[1] = "秒";
			
		} else if (djs < 60) {
			
			// 剩余时间60秒以内			
			djsStr[0] = String.valueOf(djs);
			djsStr[1] = "秒";
			
		} else {
			
			// 剩余时间1分钟以上
			if (djs % 60 == 0) {
				djsStr[0] = String.valueOf(djs / 60);
				
			} else {
				djsStr[0] = String.valueOf(djs / 60 + 1);
			}
			djsStr[1] = "分";
		}
		
		return djsStr;
	}
}
